#!/bin/sh
#Copyright (C) 2008-2009 Spark Zheng
#
#file:  reborn/mytips.sh
#usage: ./mytips.sh [parameters]
#desc:	some tips to tune up my operation system
#author:	Spark Zheng(mailto: mwishes AT gmail DOT com)
#history:
#           version 0.0.1   (initialized by Spark at 2008-12-31)
#

DEBUG=3                             #debug level: 0(none)/1/2/3(details)
LOG_FILE=/tmp/$0.${DEBUG}.log       #debug log file

T_PWD=$(pwd)
TMP_DIR=/tmp/mytips                 #tmp data dir

PATH=$PATH:/usr/lib/news/bin:/sbin:/usr/sbin:/usr/local/sbin


#function area begin

#name: usage
function usage()
{
    echo "This script need be executed in root privilege."
    echo "USAGE:$0 [option]"
	echo "      -h show this message"

    return 0
}


#function area end


#main begin
[ $DEBUG -ge 1 ] && echo "$0 begin at $(date +%c)..." >> $LOG_FILE

#getopt from cmdline
while getopts ":h" options
do
    case $options in
        h)  usage
            exit 255
            ;;
        *)  usage
            exit 254
            ;;
    esac
done

if [ $UID -ne 0 ]
then
    usage
    exit 253
fi

#mytips begin

#del_dangle_links
#usage: del_dangle_links YOUR_DIR[/usr]
function del_dangle_links()
{
	find $1 -type l ! -xtype f ! -xtype d -ok rm -f {} \;
	return 0
}

#urldecoder
#usage: urldecoder http%3A%2F%2Fwww.google.com
function urldecoder()
{
	echo "$1" | sed -e's/%\([0-9A-F][0-9A-F]\)/\\\\x\1/g' | xargs echo -e
	return 0
}

#tcp_statics
#usage: tcp_statics 80
#look for 80 port statics
function tcp_statics()
{
	/bin/netstat -an | grep ":80" | /usr/bin/awk '/^tcp/ {++S[$NF];} END {for(a in S) print a, S[a]}'
	return 0
}

#mytips end


[ $DEBUG -ge 1 ] && echo "$0 finish at $(date +%c)." >> $LOG_FILE
[ $DEBUG -ge 1 ] && echo "---------------------------------------" >> $LOG_FILE

exit 0
#main end
